import Tkinter as tk

class MainClass: 
    
    # Called once when first created
    def __init__(self):
        self.window_width = 800     # Window properties
        self.window_height = 800
        self.board = [ [1,0,1,0,1,0,1,0],
                  [0,1,0,1,0,1,0,1],
                  [1,0,1,0,1,0,1,0],
                  [0,0,0,0,0,0,0,0],
                  [0,0,0,0,0,0,0,0],
                  [0,2,0,2,0,2,0,2],
                  [2,0,2,0,2,0,2,0],
                  [0,2,0,2,0,2,0,2] ]
        self.click = 1
        self.first_row = 0 
        self.first_col = 0
        self.turn = 2
        
        
        
    def callback(self,event):
        #print "clicked at", event.x, event.y
        x = event.x
        y = event.y
        width = self.window_width
        height = self.window_height
        #print width, height
        if x <100:
            col = 0
        elif x <200:
            col = 1
        elif x <300:
            col = 2
        elif x <400:
            col = 3
        elif x <500:
            col = 4
        elif x <600:
            col = 5
        elif x <700:
            col = 6
        else :
            col = 7
        
        if y <100:
            row = 0
        elif y <200:
            row = 1
        elif y <300:
            row = 2
        elif y <400:
            row = 3
        elif y <500:
            row = 4
        elif y <600:
            row = 5
        elif y <700:
            row = 6
        else :
            row = 7
        #print row,col
        
      
    
        if (row + col) %2 == 0:
            #first click
            if self.board[row][col] >0 : #tests if user clicked on empty space
                if self.click == 1:
                    self.first_row = row
                    self.first_col = col
                    self.click = 2
                    
            #second click
            else:
                if self.turn == self.board[self.first_row][self.first_col] or self.turn+2 == self.board[self.first_row][self.first_col]: 
                    if not (row == self.first_row and col == self.first_col):#double click
                        if self.first_row - row < 3 and row - self.first_row < 3: #cant move more than 2 places
                            if self.first_col - col == 1 or self.first_col - col == 2 or col - self.first_col == 1 or col - self.first_col == 2: #only move diagonal
                                if self.first_row - row == 1 or self.first_row - row == 2 or row - self.first_row == 1 or row - self.first_row == 2: #only move diagonal
                                       if self.first_col - col < 3 and col - self.first_col < 3:
                                           # Move the piece
                                           self.board[row][col] = self.board[self.first_row][self.first_col]
                                           self.board[self.first_row][self.first_col] = 0 
                                           
                                           if self.turn == 2:
                                               self.turn = 1
                                           else:
                                               self.turn = 2
                                       if self.first_row - row == 2 or row - self.first_row == 2:#jumping function
                                              self.board[(self.first_row + row)/2][(self.first_col + col)/2] = 0
                                       
                                   
                if row == 0: #king function for black checker
                        if self.board[row][col] == 2:
                            self.board[row][col] = 4
                if row == 7:#king function for red checker
                        if self.board[row][col] == 1:
                           self.board[row][col] = 3     
                self.click = 1
                
             
       
        self.draw()
    
    # Draw the scene
    def draw(self):
        # Clear canvas
        self.w.delete("all")
        
        # Draw box
        self.w.create_rectangle(0,0,self.window_width,self.window_height,fill="white")
        row = 0
        col = 0
        self.w.create_rectangle([row*100, col*100, (row+1)*100, (col+1)*100 ],fill="red")
        row = 2
        col = 5
        self.w.create_rectangle([row*100, col*100, (row+1)*100, (col+1)*100 ],fill="black")
        row = 1
        col = 5
        self.w.create_rectangle([row*100, col*100, (row+1)*100, (col+1)*100 ],fill="red") 
        for row in range(8) :
            for col in range(8):
                if (row + col)%2 == 0:
                    self.w.create_rectangle([row*100, col*100, (row+1)*100, (col+1)*100 ],fill="white")
                else:
                    self.w.create_rectangle([row*100, col*100, (row+1)*100, (col+1)*100 ],fill="blue")
        for row in range (8):
             for col in range (8):
                 if self.board [col][row] == 0:
                     pass
                 elif self.board [col][row] == 1:
                     self.w.create_oval(row*100 + 10,col*100 + 10,row*100 + 90,col*100 + 90,fill="red") #red checker
                 elif self.board [col][row] == 2:
                     self.w.create_oval(row*100 + 10,col*100 + 10,row*100 + 90,col*100 + 90,fill="black") #black checker
                 elif self.board [col][row] == 3:
                     self.w.create_oval(row*100 + 10,col*100 + 10,row*100 + 90,col*100 + 90,fill="pink") #kinged red checker
                 elif self.board [col][row] == 4:
                     self.w.create_oval(row*100 + 10,col*100 + 10,row*100 + 90,col*100 + 90,fill="grey")#kinged black checker
                     
                 
    # Run the simulation
    def run(self):
        self.master = tk.Tk()
        self.w = tk.Canvas(self.master, width=self.window_width, height=self.window_height)
        self.w.bind("<Button-1>",self.callback)
        self.w.pack()
        self.w.after(0,self.draw)   # Will call a function after drawing.  This is EXTREMELY useful
        tk.mainloop()
        
program = MainClass()
program.run()
        



                   
               
                    
          
            
            
            
            

        
    
